package com.axdoc.baseinfo.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import org.apache.commons.lang.StringUtils;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.axdoc.baseinfo.dao.ICityMongoDao;
import com.axdoc.baseinfo.dao.IDeptMongoDao;
import com.axdoc.baseinfo.dao.IHospitalMongoDao;
import com.axdoc.baseinfo.dao.IPdeptMongoDao;
import com.axdoc.baseinfo.dao.ITitleMongoDao;
import com.axdoc.baseinfo.entity.City;
import com.axdoc.baseinfo.entity.Dept;
import com.axdoc.baseinfo.entity.Hospital;
import com.axdoc.baseinfo.entity.Pdept;
import com.axdoc.baseinfo.entity.Title;
import com.axdoc.baseinfo.exceptions.BaseInfoException;
import com.axdoc.baseinfo.service.HospitalInfoService;
import com.axdoc.common.core.pagination.PageInfo;
import com.axdoc.common.core.pagination.PageResults;

/**
 * 
 * @author liyanhui 医院相关基础信息
 */

@Service("hospitalInfoService")
@Path("hospitalInfo")
@Produces({ "application/json; charset=UTF-8", "text/xml; charset=UTF-8" })
public class HospitalInfoServiceImpl implements HospitalInfoService {

	@Autowired
	private IHospitalMongoDao hospitalMongoDao;

	@Autowired
	private IDeptMongoDao deptMongoDao;

	@Autowired
	private IPdeptMongoDao pdeptMongoDao;

	@Autowired
	private ICityMongoDao cityMongoDao;

	@Autowired
	private ITitleMongoDao titleMongoDao;

	@GET
	@POST
	@Path("/getDeptById/")
	public Dept getDeptById(@QueryParam("deptId") String deptId) throws BaseInfoException, Exception {
		if (StringUtils.isEmpty(deptId)) {
			throw new BaseInfoException(BaseInfoException.MISSING_ARGS, "缺少参数deptId");
		}
		return deptMongoDao.getDept(new ObjectId(deptId));
	}

	@GET
	@POST
	@Path("/getPdeptById/")
	public Pdept getPdeptById(@QueryParam("pdeptId") String pdeptId) throws BaseInfoException, Exception {
		if (StringUtils.isEmpty(pdeptId)) {
			throw new BaseInfoException(BaseInfoException.MISSING_ARGS, "缺少参数pdeptId");
		}
		return pdeptMongoDao.getPdept(new ObjectId(pdeptId));
	}

	@GET
	@POST
	@Path("/getHospitalById/")
	public Hospital getHospitalById(@QueryParam("hospitalId") String hospitalId) throws BaseInfoException, Exception {
		if (StringUtils.isEmpty(hospitalId)) {
			throw new BaseInfoException(BaseInfoException.MISSING_ARGS, "缺少参数hospitalId");
		}
		return hospitalMongoDao.getHospital(new ObjectId(hospitalId));
	}

	@GET
	@POST
	@Path("/findDeptPage/")
	public PageResults<Dept> findDeptPage(@QueryParam("name") String name, @QueryParam("page") String page,
			@QueryParam("pageSize") String pageSize) throws BaseInfoException, Exception {
		PageInfo pageInfo = new PageInfo();
		pageInfo.setPageNo(StringUtils.isEmpty(page) ? 1 : Integer.valueOf(page));
		pageInfo.setPageSize(StringUtils.isEmpty(pageSize) ? 20 : Integer.valueOf(pageSize));
		return deptMongoDao.findDeptPageByName(name, pageInfo);
	}

	@GET
	@POST
	@Path("/getDeptByName/")
	public Dept getDeptByName(@QueryParam("name") String name) throws BaseInfoException, Exception {
		if (StringUtils.isEmpty(name)) {
			throw new BaseInfoException(BaseInfoException.MISSING_ARGS, "缺少参数name");
		}
		return deptMongoDao.getDeptByName(name);
	}

	@GET
	@POST
	@Path("/findPdeptPage/")
	public PageResults<Pdept> findPdeptPage(@QueryParam("name") String name, @QueryParam("page") String page,
			@QueryParam("pageSize") String pageSize) throws BaseInfoException, Exception {
		PageInfo pageInfo = new PageInfo();
		pageInfo.setPageNo(StringUtils.isEmpty(page) ? 1 : Integer.valueOf(page));
		pageInfo.setPageSize(StringUtils.isEmpty(pageSize) ? 20 : Integer.valueOf(pageSize));
		return pdeptMongoDao.findPdeptPageByName(name, pageInfo);
	}

	@GET
	@POST
	@Path("/getPdeptByName/")
	public Pdept getPdeptByName(@QueryParam("name") String name) throws BaseInfoException, Exception {
		if (StringUtils.isEmpty(name)) {
			throw new BaseInfoException(BaseInfoException.MISSING_ARGS, "缺少参数name");
		}
		return pdeptMongoDao.getPdeptByName(name);
	}

	@GET
	@POST
	@Path("/findHospitalPage/")
	public PageResults<Hospital> findHospitalPage(@QueryParam("name") String name,
			@QueryParam("cityName") String cityName, @QueryParam("level") String level,
			@QueryParam("category") String category, @QueryParam("major") String major, @QueryParam("page") String page,
			@QueryParam("pageSize") String pageSize) throws BaseInfoException, Exception {
		PageInfo pageInfo = new PageInfo();
		pageInfo.setPageNo(StringUtils.isEmpty(page) ? 1 : Integer.valueOf(page));
		pageInfo.setPageSize(StringUtils.isEmpty(pageSize) ? 20 : Integer.valueOf(pageSize));
		List<String> cityIdList = null;
		if (StringUtils.isNotEmpty(cityName)) {
			List<City> clist = this.findCityList(cityName);
			cityIdList = new ArrayList<String>();
			for (City city : clist) {
				cityIdList.add(city.get_id());
			}
		}
		Hospital hospitalCon = new Hospital();
		hospitalCon.setName(name);
		hospitalCon.setLevel(level);
		hospitalCon.setCategory(StringUtils.isEmpty(category) ? null : Integer.valueOf(category));
		hospitalCon.setMajor(StringUtils.isEmpty(major) ? null : Integer.valueOf(major));
		return hospitalMongoDao.findHospitalPage(hospitalCon, cityIdList, pageInfo);
	}

	@GET
	@POST
	@Path("/getHospitalByName/")
	public Hospital getHospitalByName(@QueryParam("name") String name) throws BaseInfoException, Exception {
		if (StringUtils.isEmpty(name)) {
			throw new BaseInfoException(BaseInfoException.MISSING_ARGS, "缺少参数name");
		}
		return hospitalMongoDao.getHospitalByName(name);
	}

	@GET
	@POST
	@Path("/findHospitalListByName/")
	public List<Hospital> findHospitalListByName(@QueryParam("name") String name) throws BaseInfoException, Exception {
		if (StringUtils.isEmpty(name)) {
			throw new BaseInfoException(BaseInfoException.MISSING_ARGS, "缺少参数name");
		}
		return hospitalMongoDao.findHospitalListByName(name);
	}

	@GET
	@POST
	@Path("/getCityById/")
	public City getCityById(@QueryParam("cityId") String cityId) throws BaseInfoException, Exception {
		if (StringUtils.isEmpty(cityId)) {
			throw new BaseInfoException(BaseInfoException.MISSING_ARGS, "缺少参数cityId");
		}
		return cityMongoDao.getCity(new ObjectId(cityId));
	}

	@GET
	@POST
	@Path("/findCityPage/")
	public PageResults<City> findCityPage(@QueryParam("name") String name, @QueryParam("region") String region,
			@QueryParam("level") String level, @QueryParam("isOn") String isOn, @QueryParam("page") String page, @QueryParam("pageSize") String pageSize)
			throws BaseInfoException, Exception {
		PageInfo pageInfo = new PageInfo();
		pageInfo.setPageNo(StringUtils.isEmpty(page) ? 1 : Integer.valueOf(page));
		pageInfo.setPageSize(StringUtils.isEmpty(pageSize) ? 20 : Integer.valueOf(pageSize));
		City cityCon = new City();
		cityCon.setName(name);
		cityCon.setRegion(region);
		cityCon.setLevel(StringUtils.isEmpty(level) ? null : Integer.valueOf(level));
		cityCon.setIs_on(StringUtils.isEmpty(isOn) ? null : Integer.valueOf(isOn));
		return cityMongoDao.findCityPage(cityCon, pageInfo);
	}

	@GET
	@POST
	@Path("/findCityList/")
	public List<City> findCityList(@QueryParam("name") String name) throws BaseInfoException, Exception {
		if (StringUtils.isEmpty(name)) {
			throw new BaseInfoException(BaseInfoException.MISSING_ARGS, "缺少参数name");
		}
		return cityMongoDao.findCityListByName(name);
	}

	@GET
	@POST
	@Path("/getCityByName/")
	public City getCityByName(@QueryParam("name") String name) throws BaseInfoException, Exception {
		if (StringUtils.isEmpty(name)) {
			throw new BaseInfoException(BaseInfoException.MISSING_ARGS, "缺少参数name");
		}
		return cityMongoDao.getCityByName(name);
	}

	@GET
	@POST
	@Path("/getTitleById/")
	public Title getTitleById(@QueryParam("titleId") String titleId) throws BaseInfoException, Exception {
		if (StringUtils.isEmpty(titleId)) {
			throw new BaseInfoException(BaseInfoException.MISSING_ARGS, "缺少参数titleId");
		}
		return titleMongoDao.getTitle(new ObjectId(titleId));
	}

	@GET
	@POST
	@Path("/findTitlePage/")
	public PageResults<Title> findTitlePage(@QueryParam("name") String name, @QueryParam("page") String page,
			@QueryParam("pageSize") String pageSize) throws BaseInfoException, Exception {
		PageInfo pageInfo = new PageInfo();
		pageInfo.setPageNo(StringUtils.isEmpty(page) ? 1 : Integer.valueOf(page));
		pageInfo.setPageSize(StringUtils.isEmpty(pageSize) ? 20 : Integer.valueOf(pageSize));
		return titleMongoDao.findTitlePage(name, pageInfo);
	}

	@GET
	@POST
	@Path("/getTitleByName/")
	public Title getTitleByName(@QueryParam("name") String name) throws BaseInfoException, Exception {
		if (StringUtils.isEmpty(name)) {
			throw new BaseInfoException(BaseInfoException.MISSING_ARGS, "缺少参数name");
		}
		return titleMongoDao.getTitleByName(name);
	}
}
